﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;


public partial class Pages_City_UpdateCity : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {          
        if (!IsPostBack)
        {
            FillDdl();
            FillData();
        }  
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            City city = new City();
            CityController cityController = new CityController();
            State state = new State();
            state.Code = Convert.ToInt32(ddlState.SelectedValue);
            string id = Session["CityIdToUpdate"].ToString();

            city.Id = Convert.ToInt32(id);
            city.Description = Convert.ToString(txbCity.Text);
            city.State = state;

            if (cityController.updateCity(city) == EnumGenericState.GENERIC_STATE_SUCCESS)
            {
                Response.Write(@"<script language='javascript'>alert('Cidade alterada com sucesso!');</script>");
                Response.Redirect("CityMain.aspx"); 
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a atualização, por favor tente novamente!');</script>");
            }
       
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro ao atualizar: <br>" + ex.Message + ".');</script>");
            throw ex;
        }
        finally
        {
            Session.Remove("id");
        }
        
    }

    /// <summary>
    /// Preenche a dll com Estados
    /// </summary>
    private void FillDdl()
    {
        StateController stateController = new StateController();
        ddlState.DataSource = stateController.GetStates();
        ddlState.DataTextField = "Acronym";
        ddlState.DataValueField = "Code";
        ddlState.DataBind();
        ddlState.Items.Insert(0, "Selecione");
    }

    /// <summary>
    /// Preenche campos com os dados do objeto cidade
    /// </summary>
    private void FillData()
    {
        string id = Session["CityIdToUpdate"].ToString();
        City city = new City();
        city.Id = Convert.ToInt32(id);
        CityController cityController = new CityController();
        City newCity = new City();
        try
        {
            newCity = cityController.SelectCity(city);
            if (newCity != null)
            {
                txbCity.Text = newCity.Description;
            }
            ListItem list = ddlState.Items.FindByValue(Convert.ToString(newCity.State.Code));

            for (int i = 0; i < ddlState.Items.Count; i++)
            {
                ddlState.Items[i].Selected = false;
            }
            if (list != null)
            {
                list.Selected = true;
            }
            else
            {
                ddlState.Items[0].Selected = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro no carregamento dos estados: <br>" + ex.Message +
                               ".');</script>");
        }            
    }
}